<?php

class User_Api_User
{
	CONST FRIEND_REQUEST_IS_SENT = 2;
	CONST FRIEND_REQUEST_RECEIVED = 3;
	CONST FRIEND_REQUEST_IS_PENDDING = 4;
	CONST FRIEND_REQUEST_IS_DENIED = 5;
	CONST REQUEST_IS_APPROVED = 1;

	/**
	 * get authentiate engine
	 * @return Authenticate engine
	 */
	function getAuth()
	{
		return Nuo::getObject('user/database', 'auth');
	}

	/**
	 * authenticate user without
	 * @param array $data
	 * @return bool
	 */
	function login($data)
	{
		return $this -> getAuth() -> login($data);
	}

	/**
	 * logout authenticated.
	 */
	function logout($data = NULL)
	{
		if(isset($_SESSION['user_id'])){
			unset($_SESSION['user_id']);
		}
		
		if(isset($_SESSION['viewer_id'])){
			unset($_SESSION['viewer_id']);
		}
	}

	/**
	 * @param array $data
	 * @return $user
	 * @throws exception
	 */
	function addUser($data, $roleId = NULL)
	{

		$table = Nuo::getTable('user');

		/**
		 * check duplicated email or password.
		 */

		if (isset($data['username']))
		{

			if (is_object($table -> fetchRow($table -> select() -> where('username=?', $data['username']))))
			{
				throw new Exception('Username is duplicated');
			}
		}

		if (isset($data['email']))
		{
			if (is_object($table -> fetchRow($table -> select() -> where('email=?', $data['email']))))
			{
				throw new Exception('Email is duplicated');
			}
		}

		$resource = $table -> fetchNew();

		$resource -> fromArray($data);

		/**
		 * check for password
		 */
		if (isset($data['password']) && $data['password'])
		{
			$resource -> setPassword($data['password']);
		}
		else
		{
			$resource -> setPassword(get_random_text(6));
		}

		/**
		 * check for is verified.
		 */
		$config = Nuo::getConfig('user','signup');

		/**
		 * auto verify
		 */
		if ($config['autoVerify'])
		{
			$resource -> is_verified = 1;
		}
		else
		{
			$resource -> is_verified = 0;
		}

		/**
		 * auto approve
		 */
		if ($resource -> is_verified && $config['autoApprove'])
		{
			$resource -> is_approved = 1;
		}
		else
		{
			$resource -> is_approved = 0;
		}

		/**
		 * by default is enable
		 */
		if ($config['autoEnable'])
		{
			$resource -> is_enabled = 1;
		}
		else
		{
			$resource -> is_enabled = 0;
		}

		/**
		 * default level id = ?
		 */
		if (NULL == $roleId)
		{
			$roleId = $config['autoLevelId'];
		}

		$resource -> role_id = (int)$roleId;

		$resource -> save();

		return $resource;
	}

	/**
	 * @param model $user
	 * @param string $text
	 * @return bool
	 */
	function updateUserStatus($status, $user)
	{
		Nuo::getApi('activity') -> addFeed($poster, $poster, 'user:updateStatus', $status);
	}

	/**
	 * @param User $user
	 * @param User $friend
	 * @return bool
	 */
	function sendFriendRequest($user, $friend, $statusId = 2)
	{
		$new = $this -> _addFriendRequest($user, $friend, 2);
		if ($new)
		{
			return Nuo::getApi('request') -> addRequest($user, $friend, $user, 'user:sendFriendRequest');
		}
		return FALSE;
	}

	/**
	 * @param User $user
	 * @param User $friend
	 * @return bool
	 */
	function cancelFriendRequest($user, $resource)
	{
		$req = $this -> _getFriendRequest($user, $resource);

		if ($req)
		{
			$req -> delete();
		}
		return TRUE;
	}

	/**
	 * @param User $user
	 * @param User $friend
	 * @return bool
	 */
	function confirmFriendRequest($user, $resource)
	{
		$req = $this -> _getFriendRequest($user, $resource);

		if ($req)
		{
			$req -> status_id = self::REQUEST_IS_APPROVED;
			$req -> updated_at = time();
			$req -> save();
			/**
			 * add to friend table of two
			 */

			$activityApi = Nuo::getApi('activity');
			Nuo::getApi('notification')-> addNotification($user, $resource, 'user:beFriend');

			$activityApi -> addFeed(NULL, NULL, $resource, $user, 'user:beFriend');

			Nuo::getApi('membership') -> addMember($resource, $user);
			Nuo::getApi('membership') -> addMember($user, $resource);

			Nuo::getApi('activity') -> addFollower($resource, $user);
			Nuo::getApi('activity') -> addFollower($user, $resource);

			return TRUE;
		}

		return FALSE;
	}

	/**
	 * @param User $user
	 * @param User $receiver
	 * @return bool
	 */
	function penddingFriendRequest($user, $resource)
	{
		/**
		 * remove 2 request
		 */
		$req = $this -> _getFriendRequest($resource, $user);
		if ($req)
		{
			$req -> status_id = self::FRIEND_REQUEST_IS_PENDDING;
			$req -> updated_at = time();
			$req -> save();
			return TRUE;
		}
		return FALSE;
	}

	/**
	 * @param User $user
	 * @param User $receiver
	 * @return bool
	 */
	function denyFriendRequest($user, $resource)
	{
		/**
		 * remove 2 request
		 */
		$req = $this -> _getFriendRequest($resource, $user);
		
		if ($req)
		{
			$req -> status_id = self::FRIEND_REQUEST_IS_DENIED;
			$req -> updated_at = time();
			$req -> save();
			return TRUE;
		}
		return FALSE;
	}

	/**
	 * @param User $user
	 * @param User $friend
	 * @return bool
	 */
	protected function _addFriendRequest($user, $resource, $statusId = 2)
	{

		$table = Nuo::getTable('user/request');

		$select = $table -> select() 
		-> where('user_id=?', $user -> getId()) 
		-> where('resource_id=?', $resource -> getId());

		$req = $table -> fetchRow($select);

		if (!$req)
		{
			$req = $table -> fetchNew();
			$req -> user_id = $user -> getId();
			$req -> resource_id = $resource -> getId();
			$req -> status_id = $statusId;
			$req -> updated_at = time();
			$req -> save();
			// created
			return TRUE;
		}

		return FALSE;
	}

	protected function _updateFriendRequest($user, $resource, $statusId)
	{
		$table = Nuo::getTable('user/request');

		$select = $table -> select() -> where('user_id=?', $user -> getId()) -> where('resource_id=?', $resource -> getId());

		$req = $table -> fetchRow($select);

		if ($req)
		{
			$req -> status_id = $statusId;
			$req -> updated_at = time();
			$req -> save();
			return TRUE;
		}

		return FALSE;
	}

	protected function _getFriendRequest($user, $resource)
	{
		$table = Nuo::getTable('user/request');

		$select = $table -> select() -> where('user_id=?', $user -> getId()) -> where('resource_id=?', $resource -> getId());

		return $table -> fetchRow($select);
	}

	protected function _removeFriendRequest($user, $resource)
	{
		$resource = $this -> _getFriendRequest($user, $resource);

		if ($resource)
		{
			$resource -> delete();
			return TRUE;
		}

		return FALSE;

	}

	/**
	 * @param User $fromUser
	 * @param User $user
	 * @return int
	 * see constant for detail.
	 */
	function getStatusId($user, $resource)
	{
		if (!$user)
		{
			$user = Nuo::getUser();
		}

		if (!$user)
		{
			return 0;
		}

		// check if is member
		$isMember = Nuo::getApi('membership') -> isMember($user, $resource);

		if ($isMember)
		{
			return 1;
		}

		$table = Nuo::getTable('user/request');

		$select = $table -> select() -> where('user_id=?', $user -> getId()) -> where('resource_id=?', $resource -> getId());

		$req = $table -> fetchRow($select);
		/**
		 * has request from $user to item
		 */
		if ($req)
		{
			return $req -> status_id;
		}

		/**
		 * check friend request
		 */
		$select = $table -> select() -> where('user_id=?', $resource -> getId()) -> where('resource_id=?', $user -> getId());

		$req = $table -> fetchRow($select);

		if ($req)
		{
			return $req -> status_id;
		}

		/**
		 * never request before.
		 */
		return 0;
	}

	function getBatchStatusId($user, $ids)
	{

		return array();
	}

}
